package com.generator.statics.customerSalesStatics.service.impl;
import com.generator.util.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.generator.statics.customerSalesStatics.dao.CustomerCountMapper;
import com.generator.statics.customerSalesStatics.pojo.CustomCountCondition;
import com.generator.statics.customerSalesStatics.pojo.CustomerCount;
import com.generator.statics.customerSalesStatics.pojo.OrderOfProduct;
import com.generator.statics.customerSalesStatics.pojo.ProvinceCity;
import com.generator.statics.customerSalesStatics.service.CustomerCountService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author shkstart
 * @create 2022-10-01 15:00
 */
@Service
public class CustomerCountServiceImpl extends ServiceImpl<CustomerCountMapper, CustomerCount> implements CustomerCountService {
    @Autowired
    private CustomerCountMapper customerCountMapper;



    /**
     * @param
     * @return 分页查询客户信息
     */
    @Override
    public  PageModel02<CustomerCount> selectCustomCount02(CustomerCount customerCount,PageModel02<CustomerCount> pageModel) {
        pageModel.setCurrent(pageModel.getCurrent()<=0?1:pageModel.getCurrent());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getCurrent(),pageModel.getPageSize());
        List<CustomerCount> list = customerCountMapper.selectCustomCount2(customerCount.getCustomname());
        for (int i=0;i<list.size();i++) {
            List<OrderOfProduct> orderOfProducts = customerCountMapper.selectProductsByOrderId(list.get(i).getOrderid());
            CustomerCount count = list.get(i);
            count.setOrderNumbers(orderOfProducts.size());
            float money = 0;
            for (OrderOfProduct orderOfProduct : orderOfProducts) {
                money+=orderOfProduct.getPrototal();
            }
            count.setPrototal(money);
            count.setPrototal2(count.getPrototal());
            list.set(i,count);
        }

        PageInfo<CustomerCount> pageInfo = new PageInfo<CustomerCount>(list);
        pageModel.setRecords(new ArrayList<CustomerCount>(list));
        pageModel.setPages(pageInfo.getPages());
        pageModel.setTotal(pageInfo.getTotal());
        Set<CustomerCount> collect = pageModel.getRecords().stream().filter(distinctByKey1(s -> s.getCustomname())).collect(Collectors.toSet());
        pageModel.setRecords(new ArrayList<CustomerCount>(collect));
//        pageModel.getRecords().forEach(customerCount1 -> System.out.println(customerCount1));
        return pageModel;
    }
        /**
     * @param page
     * @return 分页查询客户信息
     */
    @Override
    public Page<CustomerCount> selectCustomCount(CustomerCount customerCount,Page<CustomerCount> page) {
        List<CustomerCount> list = customerCountMapper.selectCustomCount2(customerCount.getCustomname());
        list.stream().filter(distinctByKey1(s -> s.getCustomname()))
                .forEach(System.out::println);
        System.out.println(list);
        page.setSize(3);
        Page<CustomerCount> pageList = customerCountMapper.selectCustomCount(customerCount.getCustomname(), page);
        List<CustomerCount> records = pageList.getRecords();
        for (int i=0;i<records.size();i++) {
            List<OrderOfProduct> orderOfProducts = customerCountMapper.selectProductsByOrderId(records.get(i).getOrderid());
            CustomerCount count = records.get(i);
            count.setOrderNumbers(orderOfProducts.size());
            float money = 0;
            for (OrderOfProduct orderOfProduct : orderOfProducts) {
                money+=orderOfProduct.getPrototal();
            }
            count.setPrototal(money);
            count.setPrototal2(count.getPrototal());
            records.set(i,count);
        }
        pageList.setRecords(records);
        pageList.getRecords().stream().filter(distinctByKey1(s->s.getCustomname())).forEach(System.out::println);
        return pageList;
    }

    /**
     * @param orderId
     * @return 根据订单查询 用户
     */
    @Transactional
    @Override
    public  List<CustomerCount> selectOneCustom(String orderId) {
        return customerCountMapper.selectOneCustom(orderId);
    }

    /**
     * @param orderId
     * @return 根据订单查询
     */
    @Transactional
    @Override
    public List<OrderOfProduct> selectProductsByOrderId(String orderId) {
        return customerCountMapper.selectProductsByOrderId(orderId);
    }

    /**
     * @param customCountCondition
     * @return 多条件查询
     */
    @Transactional
    @Override
    public Page<CustomerCount> selectCustomCountByCondition(CustomCountCondition customCountCondition) {
        Page<CustomerCount> objectPage = new Page<CustomerCount>(1,3);
        return customerCountMapper.selectByMoreContion(customCountCondition, objectPage);
    }

    /**
     * @param pName
     * @return 返回省对应的城市
     */
    @Transactional
    @Override
    public List<ProvinceCity> cityByProvince(String pName) {
        return customerCountMapper.cityByProvince(pName);
    }

    static <T> Predicate<T> distinctByKey1(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

}
